#!/bin/bash -x

set -e

version="$1"
[ -z "$version" ] && {
	version=`grep Version zfs-fuse.spec | awk ' { print $2 } '`
	echo $version deduced from specfile
}
[ -z "$version" ] && {
	echo "Could not determine version from specfile"
	exit 1
}

if grep Release zfs-fuse.spec | grep -q snapshot ; then
	snapshot=`grep ^Release zfs-fuse.spec | head -1 | awk ' { print $2 } ' | cut -d . -f 3 | sed 's/snapshot//'`
fi	

BASE=`basename "$PWD"`
cd ..
rm -rf "zfs-fuse-$version"
cp -R "$BASE" "zfs-fuse-$version"
cd "zfs-fuse-$version"
rm -rf .git .gitignore zfs-fuse*tar.bz2
cd src
scons -c
rm -f .sconsign.dblite
cd ../..
tar cvjf "zfs-fuse-$version.tar.bz2" "zfs-fuse-$version"
rm -rf "zfs-fuse-$version"
mv "zfs-fuse-$version.tar.bz2" "$BASE"
cd "$BASE"
if [ -n "$snapshot" ] ; then
	mv "zfs-fuse-$version.tar.bz2" "zfs-fuse-$version-${snapshot}snapshot.tar.bz2"
fi	
